/* bmcsrp02.p - JOINT PRODUCTS COST REPORT                              */
/* Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.         */
/* $Id:: bmcsrp02.p 2366 2010-11-10 01:47:19Z zzh                          $: */
/* $Revision: 1.4.1.7 $         */
/*F0PN*/ /*V8:ConvertMode=Report                                        */
/* REVISION: 7.5      LAST MODIFIED: 01/05/95   BY: tjs *J027*          */
/* REVISION: 8.6      LAST MODIFIED: 10/20/97   BY: ays *K133*          */


/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan   */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00 BY: *N08T* Annasaheb Rahane */
/* REVISION: 9.1      LAST MODIFIED: 08/11/00 BY: *N0KK* jyn              */
/* Revision: 1.4.1.6  BY: Paul Donnelly (SB) DATE: 06/26/03 ECO: *Q00B* */
/* $Revision: 1.4.1.7 $ BY: Rajinder Kamra  DATE: 06/23/03  ECO *Q003*  */
/*-Revision end---------------------------------------------------------------*/

     {us/mf/mfdtitle.i "2+ "}

/* ********** Begin Translatable Strings Definitions ********* */

&SCOPED-DEFINE bmcsrp02_p_1 "New Page Each Base Process"
/* MaxLen: Comment: */

&SCOPED-DEFINE bmcsrp02_p_2 "As of Date"
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */

     define new shared variable site  like si_site.
     define new shared variable csset like sct_sim.
     define new shared variable bom   like pt_bom_code.
     define new shared variable bom1  like pt_bom_code.
     define new shared variable eff_date as date initial today
        label {&bmcsrp02_p_2}.
     define new shared variable newpage like mfc_logical initial no
        label {&bmcsrp02_p_1}.

     form skip(1)
        bom            colon 25   bom1 label {us/t0/t001.i} skip(1)
        eff_date       colon 30
        newpage        colon 30 skip(1)
        site           colon 30 skip(1)
        csset          colon 30
        cs_desc        colon 30
        cs_method      colon 30
        cs_type        colon 30
     with frame a side-labels width 80 attr-space.

     /* SET EXTERNAL LABELS */
     setFrameLabels(frame a:handle).

     find first icc_ctrl  where icc_ctrl.icc_domain = global_domain no-lock.

     site = global_site.
     find si_mstr no-lock  where si_mstr.si_domain = global_domain and  si_site
     = site no-error.
     if available si_mstr then csset = si_cur_set.

/*K133*/ {us/wb/wbrp01.i}
        repeat:

        if bom1 = hi_char then bom1 = "".

        do on error undo, retry with frame a:

/*K133*/ if c-application-mode <> 'web' then
        update bom bom1 eff_date newpage site csset
           with frame a editing:

          if frame-field = "site" then do:
             /* FIND NEXT/PREVIOUS RECORD */
             {us/mf/mfnp.i si_mstr site  " si_mstr.si_domain = global_domain and
             si_site "  site si_site si_site}
             if recno <> ? then do:
            site = si_site.
            display site with frame a.
            recno = ?.
             end.
          end.
          else if frame-field = "csset" then do:
             /* FIND NEXT/PREVIOUS RECORD */
             {us/mf/mfnp.i cs_mstr csset  " cs_mstr.cs_domain = global_domain and
             cs_set "  csset cs_set cs_set}
             if recno <> ? then do:
            csset = cs_set.
            display csset with frame a.
            find cs_mstr  where cs_mstr.cs_domain = global_domain and  cs_set =
            csset no-lock no-error.
            if available cs_mstr then
            display cs_desc cs_method cs_type with frame a.
            recno = ?.
             end.
          end.
          else do:
             readkey.
             apply lastkey.
          end.
           end.

/*K133*/ {us/wb/wbrp06.i &command = update &fields = "  bom bom1 eff_date newpage site
        csset" &frm = "a"}

/*K133*/ if (c-application-mode <> 'web') or
/*K133*/ (c-application-mode = 'web' and
/*K133*/ (c-web-request begins 'data')) then do:

           find si_mstr  where si_mstr.si_domain = global_domain and  si_site =
           site no-lock no-error.
           if not available si_mstr then do:
          {us/bbi/mfmsg.i 708 3} /*site does not exist*/

/*K133*/ if c-application-mode = 'web' then return.
        else next-prompt site with frame a.
          undo, retry.
           end.

           if si_db <> global_db then do:
           /* Site is not assigned to this domain */
           {us/bbi/pxmsg.i &MSGNUM=6251 &ERRORLEVEL=3}

/*K133*/ if c-application-mode = 'web' then return.
        else next-prompt site with frame a.
          undo, retry.
           end.

           if csset = "" then do:
          {us/bbi/mfmsg.i 40 3}  /* Blank not allowed */

/*K133*/ if c-application-mode = 'web' then return.
        else next-prompt csset with frame a.
          undo, retry.
           end.

           find cs_mstr  where cs_mstr.cs_domain = global_domain and  cs_set =
           csset no-lock no-error.
           if not available cs_mstr then do:
          {us/bbi/mfmsg.i 5407 3} /* Cost set does not exist */

/*K133*/ if c-application-mode = 'web' then return.
        else next-prompt csset with frame a.
          undo, retry.
           end.

           display cs_desc cs_method cs_type with frame a.
        end.

        bcdparm = "".
        {us/mf/mfquoter.i bom     }
        {us/mf/mfquoter.i bom1    }
        {us/mf/mfquoter.i eff_date}
        {us/mf/mfquoter.i newpage }
        {us/mf/mfquoter.i site    }
        {us/mf/mfquoter.i csset   }

        if bom1 = "" then bom1 = hi_char.

/*K133*/ end.

        /* SELECT PRINTER */
            {us/mf/mfselbpr.i "printer" 132}
        {us/bbi/mfphead.i}

        {us/bbi/gprun.i ""bmcsrp2a.p""}

        /* REPORT TRAILER */
        {us/mf/mfrtrail.i}

        global_site = site.
     end.

/*K133*/ {us/wb/wbrp04.i &frame-spec = a}
























/* bmcsrp2a.p - JOINT PRODUCTS COST REPORT                                    */
/* Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Id:: bmcsrp2a.p 2366 2010-11-10 01:47:19Z zzh                          $: */
/*                                                                            */
/* REVISION: 8.5      LAST MODIFIED: 02/16/95   BY: tjs *J027*                */
/* REVISION: 8.5      LAST MODIFIED: 08/10/95   BY: tjs *J06V*                */
/* REVISION: 8.6      LAST MODIFIED: 10/20/97   BY: ays *K133*                */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane          */
/* REVISION: 8.6E     LAST MODIFIED: 08/25/98   BY: *J2XC* Sandesh Mahagaokar */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan         */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00 BY: *N08T* Annasaheb Rahane     */
/* REVISION: 9.1      LAST MODIFIED: 07/04/00 BY: *N0F3* Mudit Mehta          */
/* REVISION: 9.1      LAST MODIFIED: 07/11/00   BY: *N0F9* Arul Victoria      */
/* REVISION: 9.1      LAST MODIFIED: 08/11/00   BY: *N0KK* jyn                */
/* Revision: 1.5.1.10  BY: Paul Donnelly (SB) DATE: 06/26/03 ECO: *Q00B*      */
/* Old ECO marker removed, but no ECO header exists *F0PN*                    */
/* $Revision: 1.5.1.11 $   BY: SurenderSingh Nihalani DATE: 11/28/05  ECO: *P49M*  */
/*-Revision end---------------------------------------------------------------*/
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/
/*                                                                            */
/*V8:ConvertMode=Report                                                       */

{us/bbi/mfdeclre.i}
{us/bbi/gplabel.i} /* EXTERNAL LABEL INCLUDE */

{us/wb/wbrp02.i}

define shared variable site      like si_site.
define shared variable csset     like sct_sim.
define shared variable eff_date  as date.
define shared variable bom       like pt_part.
define shared variable bom1      like pt_part.
define shared variable newpage   like mfc_logical.

define variable desc47           like pt_desc1 format "x(47)".
define variable joint_label      like lngd_translation.

define variable unit_cost      like wo_mtl_tot
   label "Unit Cost" no-undo.
define variable set_unit_cost  like wo_mtl_tot
   label "Unit Cost" no-undo.
define variable batch_cost     like wo_mtl_tot
   label "Batch Cost" no-undo.

define variable jp_batch_cost    like batch_cost.
define variable net_batch_cost   like batch_cost.
define variable tot_lit          as character format "x(45)".
define variable underscore       as character format "x(15)"
   initial "---------------".
define variable by_tot_cost      like batch_cost
   format "->>>,>>>,>>9.99".
define variable co_tot_cost1     like batch_cost
   format "->>>,>>>,>>9.99".
define variable co_tot_cost2     like batch_cost.
define variable batch_size       like bom_batch.
define variable batch_um         like bom_batch_um.
define variable qty_per          like bom_batch.
define variable jp_batch_qty     like pt_batch.
define variable cst_pct          like ps_cst_pct.
define variable prod_pct         like ps_prod_pct
   column-label "Process!Percent".
define variable heading_reqd     like mfc_logical extent 3.
define variable bom_code         like pt_bom_code.
define variable blnk             like wo_part
   initial "                  ".
define variable len              as integer.
define variable pass             as integer.

define variable calc_unit_cost      like wo_mtl_tot no-undo.

define buffer ptmstr for pt_mstr.
define buffer pt_mstr1 for pt_mstr.
define buffer bommstr for bom_mstr.

form
   site csset
with frame pageheader width 134 side-labels
no-attr-space page-top.

/* SET EXTERNAL LABELS */
setFrameLabels(frame pageheader:handle).

form
   joint_label no-label skip(0)
with frame joint_label width 134
no-attr-space.

form
   pt_part
   desc47
   bom_batch_um
   batch_size
   unit_cost
   batch_cost
with frame base_det width 134 no-attr-space.

/* SET EXTERNAL LABELS */
setFrameLabels(frame base_det:handle).

form
   pt_part at 7
   pt_desc1
   pt_um
   prod_pct
   ps_qty_type
   jp_batch_qty
   unit_cost at 85
   jp_batch_cost
with down frame by_det width 134 no-attr-space.

/* SET EXTERNAL LABELS */
setFrameLabels(frame by_det:handle).

form
   pt_part at 7
   pt_desc1
   pt_um
   prod_pct
   ps_qty_type
   jp_batch_qty
   cst_pct
   calc_unit_cost column-label "Calculated!Unit Cost"
   jp_batch_cost
   set_unit_cost
with down frame co_det width 134 no-attr-space.

/* SET EXTERNAL LABELS */
setFrameLabels(frame co_det:handle).

form
   tot_lit                  to  90 no-label
   co_tot_cost1             to 115 no-label
with down frame co_tot width 134 no-attr-space.

display site csset with frame pageheader.

for each bom_mstr
   where bom_mstr.bom_domain  = global_domain
   and   bom_parent          >= bom
   and   bom_parent          <= bom1
no-lock:

   /* GET BASE ITEM-SITE DETAIL & ITEM MASTER */
   find ptp_det
      where ptp_det.ptp_domain = global_domain
      and   ptp_part           = bom_parent
      and   ptp_site           = site
   no-lock no-error.

   if available ptp_det
      and ptp_joint_type <> "5"
   then
      next.

   find pt_mstr
      where pt_mstr.pt_domain = global_domain
      and   pt_part           = bom_parent
   no-lock no-error.

   if not available pt_mstr
   then
      next.

   if not available ptp_det
      and pt_joint_type <> "5"
   then
      next.

   assign
      desc47       = pt_desc1 + " " + pt_desc2
      heading_reqd = yes
      batch_size   = bom_batch.

   if batch_size = 0
   then
      batch_size = 1.

   batch_um = bom_batch_um.

   /* GET sct_det COST RECORD */

   find sct_det
      where sct_det.sct_domain = global_domain
      and   sct_sim            = csset
      and   sct_part           = pt_part
      and   sct_site           = site
   no-lock no-error.

   if not available sct_det
   then
      next.

   batch_cost = sct_cst_tot * batch_size.

   if line-counter + 4 > page-size
   then
      page.

   display
      getTermLabel("BASE_PROCESS",24) @ joint_label
   with frame joint_label.

   display
      pt_part
      desc47
      bom_batch_um
      batch_size
      sct_cst_tot @ unit_cost
      batch_cost
   with frame base_det.

   /* BY-PRODUCT */
   by_tot_cost = 0.
   for each ps_mstr
      where ps_mstr.ps_domain  = global_domain
      and   (ps_comp           = bom_parent
             and ps_ps_code    = "J"
             and (eff_date = ?
                  or (eff_date         <> ?
                      and (ps_start     = ?
                           or ps_start <= eff_date)
                      and (ps_end       = ?
                           or eff_date <= ps_end)))
             and ps_joint_type > "1"
             and ps_joint_type < "5")
   use-index ps_comp
   no-lock:

      find pt_mstr
         where pt_mstr.pt_domain = global_domain
         and   pt_part           = ps_par
      no-error.

      /* GET sct_det COST RECORD */

      find sct_det
         where sct_det.sct_domain = global_domain
         and   sct_sim            = csset
         and   sct_part           = pt_part
         and   sct_site           = site
      no-lock no-error.

      if available sct_det
      then
         unit_cost = sct_cst_tot.
      else
         unit_cost = 0.

      prod_pct = ps_prod_pct.

      if heading_reqd[3] = yes
      then do:

         if line-counter + 5 > page-size
         then
            page.

         display
            "  " + getTermLabel("BY-PRODUCTS",22) @ joint_label
         with frame joint_label.
         heading_reqd[3] = no.

      end. /* IF heading_reqd[3] = yes */

      assign
         jp_batch_qty  = ps_cop_qty  * (if ps_qty_type = ""
                                        then
                                           batch_size
                                        else
                                           1)
         jp_batch_cost = unit_cost   * jp_batch_qty
         by_tot_cost   = by_tot_cost + jp_batch_cost.

      display
         pt_part
         pt_desc1
         pt_um
         prod_pct when (prod_pct <> 0)
         ps_qty_type
         jp_batch_qty
         unit_cost
         jp_batch_cost
      with frame by_det.
      down 1 with frame by_det.

   end. /* FOR EACH ps_mstr */

   /* BY-PRODUCT TOTAL DISPLAY */
   if by_tot_cost <> 0
   then do:

      net_batch_cost = max(0,batch_cost - by_tot_cost).

      if line-counter + 3 > page-size
      then
         page.

      display
         underscore @ jp_batch_cost
      with frame by_det.
      down 1 with frame by_det.

      display
         getTermLabel("TOTAL",14) + ":" @ unit_cost
         by_tot_cost @ jp_batch_cost
      with frame by_det.

      display
         getTermLabelRtColon("NET_BASE_PROCESS_COST",45) @ tot_lit
         net_batch_cost @ co_tot_cost1
      with frame co_tot.

   end. /* IF by_tot_cost <> 0 */
   else
      net_batch_cost = batch_cost.

   assign
      co_tot_cost1 = 0
      co_tot_cost2 = 0
      pass         = 1.

   do while pass < 3:
      /* CO-PRODUCTS LISTED TWICE - 1ST WITH THOSE WITH PLANNING    */
      /* BOM EQUAL TO BASE, THEN THOSE WITH PLANNING BOM NOT EQUAL. */
      for each ps_mstr
         where ps_mstr.ps_domain  = global_domain
         and   (ps_comp           = bom_parent
                and (eff_date     = ?
                     or (eff_date         <> ?
                         and (ps_start     = ?
                              or ps_start <= eff_date)
                         and (ps_end       = ?
                              or eff_date <= ps_end)))
                and ps_ps_code    = "J"
                and ps_joint_type = "1")
      use-index ps_comp
      no-lock:

         find pt_mstr
            where pt_mstr.pt_domain = global_domain
            and   pt_part           = ps_par
         no-error.

         find ptp_det
            where ptp_det.ptp_domain = global_domain
            and   ptp_part           = ps_par
            and   ptp_site           = site
         no-error.

         if pass = 1
            and ((available ptp_det
                     and ptp_bom_code <> bom_parent)
                 or (not available ptp_det
                        and pt_bom_code <> bom_parent))
         then
            next.

         if pass = 2
            and ((available ptp_det
                     and ptp_bom_code = bom_parent)
                 or (not available ptp_det
                        and pt_bom_code = bom_parent))
            then next.

         assign
            cst_pct  = ps_cst_pct
            prod_pct = ps_prod_pct.

         if ps_qty_type = ""
         then
            jp_batch_qty = ps_cop_qty * batch_size.
         else
            jp_batch_qty = ps_cop_qty.

         assign
            jp_batch_cost = net_batch_cost * (cst_pct / 100)
            unit_cost     = jp_batch_cost / jp_batch_qty.

         if pass = 1
         then do:

            if heading_reqd[1] = yes
            then do:

               if line-counter + 6 > page-size
               then
                  page.

               display
                  "  " + getTermLabel("CO-PRODUCTS",22) @ joint_label
               with frame joint_label.
               heading_reqd[1] = no.

            end. /* IF heading_reqd[1] = yes */

            co_tot_cost1 = co_tot_cost1 + jp_batch_cost.

         end. /* IF pass = 1 */

         if pass = 2
         then do:

            if heading_reqd[2] = yes
            then do:

               if line-counter + 6 > page-size
               then
                  page.

               display
                  "  " + getTermLabel("CO-PRDCTS_PLANNED_FROM_ANOTHER_BASE",40)
                  @ joint_label format "x(40)"
               with frame joint_label.
               heading_reqd[2] = no.

            end. /* IF heading_reqd[2] = yes */

            co_tot_cost2 = co_tot_cost2 + jp_batch_cost.

         end. /* IF pass = 2 */

         /* GET sct_det COST RECORD */

         find sct_det
            where sct_det.sct_domain = global_domain
            and   sct_sim            = csset
            and   sct_part           = pt_part
            and   sct_site           = site
         no-lock no-error.

         if available sct_det
         then
            set_unit_cost = sct_cst_tot.
         else
            set_unit_cost = 0.

         calc_unit_cost = unit_cost.

         if line-counter + 3 > page-size
         then
            page.

         display
            pt_part
            pt_desc1
            pt_um
            prod_pct when (prod_pct <> 0)
            ps_qty_type
            jp_batch_qty
            cst_pct when (cst_pct <> 0)
            calc_unit_cost
            jp_batch_cost
            set_unit_cost
         with frame co_det.
         down 1 with frame co_det.

         /* RIGHT JUSTIFY BOM CODE */
         if pass = 2
         then do:

            if available ptp_det
            then
               bom_code = ptp_bom_code.
            else
               bom_code = pt_bom_code.

            assign
               len      = length(bom_code)
               bom_code = substring(blnk,1,(18 - len)) +
                          substring(bom_code,1,(len)).

            display
               bom_code @ pt_part
            with frame co_det.
            down 1 with frame co_det.

         end. /* IF pass = 2 */
      end. /* FOR EACH ps_mstr */

      /* CO-PRODUCT SUB-TOTAL DISPLAY */
      if (pass = 1
             and co_tot_cost1 <> 0)
         or (pass = 2
                and co_tot_cost2 <> 0)
      then do:

         if line-counter + 5 > page-size
         then
            page.

         display
            underscore @ jp_batch_cost
         with frame co_det.
         down 1 with frame co_det.

         if pass = 1
         then
            jp_batch_cost = co_tot_cost1.
         else
            jp_batch_cost = co_tot_cost2.

         display
            getTermLabel("TOTAL",14) + ":" @ calc_unit_cost
            jp_batch_cost
         with frame co_det.

      end. /* IF (pass = 1 */

      /* CO-PRODUCT GRAND TOTAL FOR BASE DISPLAY */
      if pass              = 2
         and co_tot_cost1 <> 0
         and co_tot_cost2 <> 0
      then do:

         co_tot_cost1 = co_tot_cost1 + co_tot_cost2.

         display
            getTermLabelRtColon("TOTAL_CO-PRODUCT_COST",45) @ tot_lit
            co_tot_cost1
         with frame co_tot.

      end. /* IF pass = 2 */
      pass = pass + 1.

   end. /* do while pass */

   if newpage
   then
      page.
   else
      put skip(1).

   {us/mf/mfrpchk.i}
end. /* FOR EACH bom_mstr */
{us/wb/wbrp04.i}

